home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE13 / COPYICON.C < prev    next >
C/C++ Source or Header  |  1996-01-31  |  7KB  |  208 lines

  1.  
  2. #include <windows.h>  
  3. #include "CopyIcon.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HICON hIcon = NULL;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_PAINT   : 
  114.               {
  115.                  PAINTSTRUCT ps;
  116.  
  117.                  BeginPaint( hWnd, &ps );
  118.  
  119.                  // Paint the icon if we have one.
  120.                  //...............................
  121.                  if ( hIcon )
  122.                     DrawIcon( ps.hdc, 10, 10, hIcon );
  123.  
  124.                  EndPaint( hWnd, &ps );
  125.               }
  126.               break;
  127.  
  128.       case WM_COMMAND :
  129.               switch( LOWORD( wParam ) )
  130.               {
  131.                  case IDM_TEST :
  132.                         {
  133.                            // Find Excel's main window.
  134.                            //..........................
  135.                            HWND hExWnd  = FindWindow( "XLMAIN", NULL );
  136.                            if ( hExWnd )
  137.                            {
  138.                               HINSTANCE hExInst;
  139.                               HICON     hExIcon; 
  140.                              
  141.                               // Retrieve the instance handle to excel
  142.                               // and load an icon from excel.
  143.                               //......................................
  144.                               hExInst = (HINSTANCE)GetWindowLong( hExWnd,
  145.                                                               GWL_HINSTANCE );
  146.                               hExIcon = LoadIcon( hExInst, 
  147.                                                   MAKEINTRESOURCE( 261 ) );
  148.  
  149.                               // Make a copy of the icon for 
  150.                               // this application to use.
  151.                               //............................
  152.                               hIcon = CopyIcon( hExIcon );
  153.  
  154.                               InvalidateRect( hWnd, NULL, TRUE );
  155.                               UpdateWindow( hWnd );
  156.                            }
  157.                            else
  158.                               MessageBox( hWnd, "Excel is not loaded",
  159.                                           "Message",
  160.                                           MB_OK | MB_ICONINFORMATION );
  161.                         }
  162.                         break;
  163.  
  164.                  case IDM_ABOUT :
  165.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  166.                         break;
  167.  
  168.                  case IDM_EXIT :
  169.                         DestroyWindow( hWnd );
  170.                         break;
  171.               }
  172.               break;
  173.       
  174.       case WM_DESTROY :
  175.               PostQuitMessage(0);
  176.               break;
  177.  
  178.       default :
  179.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  180.    }
  181.  
  182.    return( 0L );
  183. }
  184.  
  185.  
  186. LRESULT CALLBACK About( HWND hDlg,           
  187.                         UINT message,        
  188.                         WPARAM wParam,       
  189.                         LPARAM lParam)
  190. {
  191.    switch (message) 
  192.    {
  193.        case WM_INITDIALOG: 
  194.                return (TRUE);
  195.  
  196.        case WM_COMMAND:                              
  197.                if (   LOWORD(wParam) == IDOK         
  198.                    || LOWORD(wParam) == IDCANCEL)    
  199.                {
  200.                        EndDialog(hDlg, TRUE);        
  201.                        return (TRUE);
  202.                }
  203.                break;
  204.    }
  205.  
  206.    return (FALSE); 
  207. }
  208.